package com.stevenmoon.csdnplus.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.FrameLayout;

import com.stevenmoon.csdnplus.R;
import com.stevenmoon.csdnplus.activity.base.BaseActivity;
import com.stevenmoon.csdnplus.app.LoginPrefs;
import com.stevenmoon.csdnplus.event.SplashFinishedEvent;
import com.stevenmoon.csdnplus.fragment.SplashStarWarFragment;
import com.stevenmoon.csdnplus.logic.ProfileManager;
import com.stevenmoon.csdnplus.util.TipUtil;

import org.simple.eventbus.Subscriber;

import butterknife.Bind;

public class SplashActivity extends BaseActivity {


    @Bind(R.id.container)
    FrameLayout mContainer;

    @Override
    protected void initViews() {
        getSupportFragmentManager().beginTransaction().replace(R.id.container,
                SplashStarWarFragment.newInstance()).commit();
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_splash;
    }

    void goLogin(){
        finish();
        LoginActivity.goToLogin(this);
    }

    void goMain(){
        finish();
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
    }

    @Subscriber
    private void processTransition(SplashFinishedEvent event){
        ProfileManager.getUC(LoginPrefs.getUserName());
        if(TextUtils.isEmpty(LoginPrefs.getSessionId())){//如果未登录
            goLogin();
        }else{
            try {
                //如果过期了，则去自动登录
                long expire = Long.parseLong(LoginPrefs.getSessionExpired());
                if(expire < System.currentTimeMillis()){
                    TipUtil.showToast("过期了，自动登录中");
                    //TODO auto login
                    goLogin();
                }else{
                    //goLogin();
                    goMain();
                   // goMain();
                }
            }catch (NumberFormatException e){
                goLogin();
            }

        }


    }

}
